package com.yinhai.util;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;

import com.yinhai.activity.R;


public class AccoutTypeutils {

	  public interface StringInflater {
	        public CharSequence inflateUsing(Context context, ContentValues values);
	    }
	  public static abstract class CommonInflater implements StringInflater {
	        protected abstract int getTypeLabelResource(Integer type);

	        protected boolean isCustom(Integer type) {
	            return type == BaseTypes.TYPE_CUSTOM;
	        }

	        protected String getTypeColumn() {
	            return Phone.TYPE;
	        }

	        protected String getLabelColumn() {
	            return Phone.LABEL;
	        }

	        protected CharSequence getTypeLabel(Resources res, Integer type, CharSequence label) {
	            final int labelRes = getTypeLabelResource(type);
	            if (type == null) {
	                return res.getText(labelRes);
	            } else if (isCustom(type)) {
	                return res.getString(labelRes, label == null ? "" : label);
	            } else {
	                return res.getText(labelRes);
	            }
	        }

	        @Override
	        public CharSequence inflateUsing(Context context, ContentValues values) {
	            final Integer type = values.getAsInteger(getTypeColumn());
	            final String label = values.getAsString(getLabelColumn());
	            return getTypeLabel(context.getResources(), type, label);
	        }

	        @Override
	        public String toString() {
	            return this.getClass().getSimpleName();
	        }
	    }
	  
	  
	  /**
	   * 电话
	   * @author Administrator
	   *
	   */
	  public static class PhoneActionInflater extends CommonInflater {
	        @Override
	        protected boolean isCustom(Integer type) {
	            return type == Phone.TYPE_CUSTOM || type == Phone.TYPE_ASSISTANT;
	        }

	        @Override
	        protected int getTypeLabelResource(Integer type) {
	            if (type == null) return R.string.call_other;
	            switch (type) {
	                case Phone.TYPE_CUSTOM: return R.string.call_custom;
	                case Phone.TYPE_HOME: return R.string.call_home;
	                case Phone.TYPE_MOBILE: return R.string.call_mobile;
	                case Phone.TYPE_WORK: return R.string.call_work;
	                case Phone.TYPE_FAX_WORK: return R.string.call_fax_work;
	                case Phone.TYPE_FAX_HOME: return R.string.call_fax_home;
	                case Phone.TYPE_PAGER: return R.string.call_pager;
	                case Phone.TYPE_OTHER: return R.string.call_other;
	                case Phone.TYPE_CALLBACK: return R.string.call_callback;
	                case Phone.TYPE_CAR: return R.string.call_car;
	                case Phone.TYPE_COMPANY_MAIN: return R.string.call_company_main;
	                case Phone.TYPE_ISDN: return R.string.call_isdn;
	                case Phone.TYPE_MAIN: return R.string.call_main;
	                case Phone.TYPE_OTHER_FAX: return R.string.call_other_fax;
	                case Phone.TYPE_RADIO: return R.string.call_radio;
	                case Phone.TYPE_TELEX: return R.string.call_telex;
	                case Phone.TYPE_TTY_TDD: return R.string.call_tty_tdd;
	                case Phone.TYPE_WORK_MOBILE: return R.string.call_work_mobile;
	                case Phone.TYPE_WORK_PAGER: return R.string.call_work_pager;
	                case Phone.TYPE_ASSISTANT: return R.string.call_assistant;
	                case Phone.TYPE_MMS: return R.string.call_mms;
	                default: return R.string.call_custom;
	            }
	        }
	    }	  
	  
	  /**
	   * 邮箱
	   * @author Administrator
	   *
	   */
	    public static class EmailActionInflater extends CommonInflater {
	        @Override
	        protected int getTypeLabelResource(Integer type) {
	            if (type == null) return R.string.email;
	            switch (type) {
	                case Email.TYPE_HOME: return R.string.email_home;
	                case Email.TYPE_WORK: return R.string.email_work;
	                case Email.TYPE_OTHER: return R.string.email_other;
	                case Email.TYPE_MOBILE: return R.string.email_mobile;
	                case Email.TYPE_CUSTOM: return R.string.email_custom;
	                default: return R.string.email_custom;
	            }
	        }
	    }
	    /**
	     * IM
	     * @author Administrator
	     *
	     */
	    public static class ImActionInflater extends CommonInflater {
	        @Override
	        protected String getTypeColumn() {
	            return Im.PROTOCOL;
	        }

	        @Override
	        protected String getLabelColumn() {
	            return Im.CUSTOM_PROTOCOL;
	        }

	        @Override
	        protected int getTypeLabelResource(Integer type) {
	            if (type == null) return R.string.chat;
	            switch (type) {
	                case Im.PROTOCOL_AIM: return R.string.chat_aim;
	                case Im.PROTOCOL_MSN: return R.string.chat_msn;
	                case Im.PROTOCOL_YAHOO: return R.string.chat_yahoo;
	                case Im.PROTOCOL_SKYPE: return R.string.chat_skype;
	                case Im.PROTOCOL_QQ: return R.string.chat_qq;
	                case Im.PROTOCOL_GOOGLE_TALK: return R.string.chat_gtalk;
	                case Im.PROTOCOL_ICQ: return R.string.chat_icq;
	                case Im.PROTOCOL_JABBER: return R.string.chat_jabber;
	                case Im.PROTOCOL_NETMEETING: return R.string.chat;
	                case Im.PROTOCOL_CUSTOM: return R.string.map_custom;
	                default: return R.string.map_custom;
	            }
	        }
	    }
	    
	    /**
	     * 地址
	     * @author Administrator
	     *
	     */
	    public static class PostalActionInflater extends CommonInflater {
	        @Override
	        protected int getTypeLabelResource(Integer type) {
	            if (type == null) return R.string.map_other;
	            switch (type) {
	                case StructuredPostal.TYPE_HOME: return R.string.map_home;
	                case StructuredPostal.TYPE_WORK: return R.string.map_work;
	                case StructuredPostal.TYPE_OTHER: return R.string.map_other;
	                case StructuredPostal.TYPE_CUSTOM: return R.string.map_custom;
	                default: return R.string.map_custom;
	            }
	        }
	    }
	    /**
	     * 生日
	     * @author Administrator
	     *
	     */
	    public static class BirhdayActionInflater extends CommonInflater {
	    	@Override
	    	protected int getTypeLabelResource(Integer type) {
	    		if (type == null) return R.string.map_other;
	    		switch (type) {
	    		case Event.TYPE_BIRTHDAY: return R.string.birthday;
	    		case Event.TYPE_ANNIVERSARY: return R.string.birthday_remenber;
	    		case Event.TYPE_OTHER: return R.string.birthday_custom;
	    		default: return R.string.birthday_custom;
	    		}
	    	}
	    }

	    
	    public static List<DataKind> getPhoneList(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	PhoneActionInflater p = new PhoneActionInflater();
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_HOME)),Phone.TYPE_HOME));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_MOBILE)),Phone.TYPE_MOBILE));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_WORK)),Phone.TYPE_WORK));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_FAX_WORK)),Phone.TYPE_FAX_WORK));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_FAX_HOME)),Phone.TYPE_FAX_HOME));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_PAGER)),Phone.TYPE_PAGER));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_OTHER)),Phone.TYPE_OTHER));
	    	arrays.add(new DataKind(AccountMintype.PHONE,contex.getResources().getString(p.getTypeLabelResource(Phone.TYPE_CUSTOM)),Phone.TYPE_CUSTOM));
	    	
	    	return arrays;
	    }
	    public static List<DataKind> getEmailList(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	EmailActionInflater p = new EmailActionInflater();
	    	arrays.add(new DataKind(AccountMintype.EMAIL,contex.getResources().getString(p.getTypeLabelResource(Email.TYPE_HOME)),Email.TYPE_HOME));
	    	arrays.add(new DataKind(AccountMintype.EMAIL,contex.getResources().getString(p.getTypeLabelResource(Email.TYPE_MOBILE)),Email.TYPE_MOBILE));
	    	arrays.add(new DataKind(AccountMintype.EMAIL,contex.getResources().getString(p.getTypeLabelResource(Email.TYPE_WORK)),Email.TYPE_WORK));
	    	arrays.add(new DataKind(AccountMintype.EMAIL,contex.getResources().getString(p.getTypeLabelResource(Email.TYPE_OTHER)),Email.TYPE_OTHER));
	    	arrays.add(new DataKind(AccountMintype.EMAIL,contex.getResources().getString(p.getTypeLabelResource(Email.TYPE_CUSTOM)),Email.TYPE_CUSTOM));
	    	
	    	return arrays;
	    }
	    public static List<DataKind> getIMList(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	ImActionInflater p = new ImActionInflater();
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_AIM)),Im.TYPE_HOME));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_MSN)),Im.PROTOCOL_MSN));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_YAHOO)),Im.TYPE_WORK));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_SKYPE)),Im.PROTOCOL_SKYPE));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_QQ)),Im.PROTOCOL_QQ));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_GOOGLE_TALK)),Im.PROTOCOL_GOOGLE_TALK));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_ICQ)),Im.PROTOCOL_ICQ));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_JABBER)),Im.PROTOCOL_JABBER));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_NETMEETING)),Im.PROTOCOL_NETMEETING));
	    	arrays.add(new DataKind(AccountMintype.IM,contex.getResources().getString(p.getTypeLabelResource(Im.PROTOCOL_CUSTOM)),Im.PROTOCOL_CUSTOM));
	    	
	    	return arrays;
	    }
	    public static List<DataKind> getAddrList(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	PostalActionInflater p = new PostalActionInflater();
	    	arrays.add(new DataKind(AccountMintype.ADDR,contex.getResources().getString(p.getTypeLabelResource(StructuredPostal.TYPE_HOME)),StructuredPostal.TYPE_HOME));
	    	arrays.add(new DataKind(AccountMintype.ADDR,contex.getResources().getString(p.getTypeLabelResource(StructuredPostal.TYPE_WORK)),StructuredPostal.TYPE_WORK));
	    	arrays.add(new DataKind(AccountMintype.ADDR,contex.getResources().getString(p.getTypeLabelResource(StructuredPostal.TYPE_OTHER)),StructuredPostal.TYPE_OTHER));
	    	arrays.add(new DataKind(AccountMintype.ADDR,contex.getResources().getString(p.getTypeLabelResource(StructuredPostal.TYPE_CUSTOM)),StructuredPostal.TYPE_CUSTOM));
	    	
	    	return arrays;
	    }
	    public static List<DataKind> getBirthday(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	BirhdayActionInflater p = new BirhdayActionInflater();
	    	arrays.add(new DataKind(AccountMintype.identity,contex.getResources().getString(p.getTypeLabelResource(Event.TYPE_BIRTHDAY)),Event.TYPE_BIRTHDAY));
	    	arrays.add(new DataKind(AccountMintype.identity,contex.getResources().getString(p.getTypeLabelResource(Event.TYPE_ANNIVERSARY)),Event.TYPE_ANNIVERSARY));
	    	arrays.add(new DataKind(AccountMintype.identity,contex.getResources().getString(p.getTypeLabelResource(Event.TYPE_OTHER)),Event.TYPE_OTHER));
	    	
	    	return arrays;
	    }
	    //orgnazition
	    public static List<DataKind> getOrgnization(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	arrays.add(new DataKind(AccountMintype.organization,contex.getResources().getString(R.string.company),0));
	    	
	    	return arrays;
	    }
	    //nicknam
	    public static List<DataKind> getNickName(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	arrays.add(new DataKind(AccountMintype.nickname,contex.getResources().getString(R.string.nice_name),0));
	    	
	    	return arrays;
	    }
	    //note
	    public static List<DataKind> getBak(Context contex){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	arrays.add(new DataKind(AccountMintype.BAK,contex.getResources().getString(R.string.user_bak),0));
	    	
	    	return arrays;
	    }
	    //group
	    public static List<DataKind> getGroups(Context context){
	    	List<DataKind> arrays = new ArrayList<DataKind>();
	    	  Cursor cursor = context.getContentResolver().query(
						ContactsContract.Groups.CONTENT_URI, null, null, null, null);
	           if(cursor.moveToFirst()) {
	        	   do{
	               String name = cursor.getString( cursor.getColumnIndex("title"));
	               String id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership._ID));
	               DataKind datakind = new DataKind(AccountMintype.GROUP, name, Integer.parseInt(id));
	               arrays.add(datakind);
	        	   } while(cursor.moveToNext());
	           }
	           cursor.close();
	           return arrays;
	    }

}
